package sr.codemg.controller;

import io.jsonwebtoken.Claims;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import sr.codemg.config.JwtConfig;
import sr.codemg.config.RedisUtil;
import sr.codemg.service.LoginService;
import sr.codemg.service.impl.SysMenusServiceImpl;
import sr.codemg.utils.Constants;
import sr.codemg.utils.ResponseCode;
import sr.codemg.utils.TokenManager;
import sr.codemg.vm.Base;
import sr.codemg.vm.Login;
import sr.codemg.vm.Passwd;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Slf4j
@RestController
@RequestMapping("/authorized")
public class SysAuthorizedController {

    @Autowired
    LoginService loginService;

    @Autowired
    RedisUtil redisUtil;

    @PostMapping
    public Base.Response authorized(@RequestBody Login.Request entity, HttpServletResponse response) {
        if (StringUtils.isEmpty(entity.getUserId()) || StringUtils.isEmpty(entity.getPasswd()))
            return ResponseCode.getBase("00001");
        return loginService.login(entity, response);
    }

    @PostMapping("/refresh")
    public Base.Response refresh(@RequestBody Base.Request entity) {
        if (StringUtils.isEmpty(entity.getToken())) return ResponseCode.getBase("00400");

        Claims claims = JwtConfig.thinning(entity.getToken());
        if (claims == null) return ResponseCode.getBase("00005");
        String jwtToken = JwtConfig.refresh(claims);
        redisUtil.hset(JwtConfig.getUserId(claims), Constants.USER_CONFIG.JWT_TOKENS, jwtToken);
        redisUtil.set(jwtToken, JwtConfig.getUserId(claims));
        return new Base.Response(Constants.SUCC, jwtToken);
    }

    @PutMapping
    public Base.Response update(@RequestBody Passwd.Request entity, HttpServletRequest req, HttpServletResponse res,
                                @CookieValue(Constants.Authorization) String token) {
        if (StringUtils.isEmpty(entity.getPasswd()) || StringUtils.isEmpty(entity.getNewPasswd()))
            return ResponseCode.getBase("00400");

        Base.Response result = loginService.updatePass(entity);
        if (Constants.SUCC.equals(result.getCode())) {
            TokenManager.delCookie(req, res, Constants.Authorization);
            String userId = entity.getUserId();
            loginService.deleteUserCache(userId, token);
        }
        return result;
    }

    @DeleteMapping
    public Base.Response logout(HttpServletRequest req, HttpServletResponse res, @CookieValue(Constants.Authorization) String token) {
        String userId = JwtConfig.thinning(token).getSubject();
        String key = SysMenusServiceImpl.class.getName() + userId;
        redisUtil.del(key);
        loginService.deleteUserCache(userId, token);
        TokenManager.delCookie(req, res, Constants.Authorization);
        return ResponseCode.getBase(ResponseCode.SUCCESS);
    }

}